package flown.window.graphics.ui;

import flown.window.graphics.Renderer2D;
import flown.window.graphics.ui.event.Event;
import flown.window.graphics.ui.listener.ActionListener;
import flown.window.graphics.ui.listener.WidgetListener;

import java.awt.Rectangle;


/**
 * @author flown
 */

public class Widget
{
    protected Rectangle rect = new Rectangle();;///< x,y,width,height
    protected Container at = null;
    protected WidgetListener listener_widget = null;
    protected ActionListener listener_action = null;

    public Widget() { this(0, 0, 0, 0); }
    public Widget(int width, int height) { this(0, 0, width, height); }
    public Widget(int x, int y, int width, int height)
    {
        this.listener_widget = flown.window.graphics.ui.Widget.NewVoidWidgetListener();
        this.listener_action = flown.window.graphics.ui.Widget.NewVoidActionListener();
        this.SetBounds(x, y, width, height);
    }

    public static WidgetListener NewVoidWidgetListener()
    {
        return new WidgetListener()
        {
        @Override public void WidgetResized(Event e) {}
        @Override public void WidgetMoved(Event e) {}
        @Override public void WidgetShown(Event e) {}
        @Override public void WidgetHidden(Event e) {}
        };
    }

    public static ActionListener NewVoidActionListener()
    {
        return new ActionListener()
        {
            @Override public void click(Event e) {}
            @Override public void press(Event e) {}
            @Override public void up(Event e) {}
        };
    }


    public void SetBounds(int x, int y, int width, int height)
    {
        this.rect.x = x;
        this.rect.y = y;
        this.rect.width = width;
        this.rect.height = height;
    }

    public final int GetX() { return this.rect.x; }
    public final int GetY() { return this.rect.y; }
    public final int GetWidth() { return this.rect.width; }
    public final int GetHeight() { return this.rect.height; }


    public final void SetWidgetListener(WidgetListener listener) { this.listener_widget = listener; }
    public final void SetActionListener(ActionListener listener) { this.listener_action = listener; }


    /**
     *
     * @param widget Widget
     * @return 是否相交
     */
    public final boolean Intersects(Widget widget) { return this.rect.intersects(widget.rect); }

    /**
     *
     * @param r Rectangle
     * @return 是否相交
     */
    public final boolean Intersects(Rectangle r) { return this.rect.intersects(r); }

    /**
     *
     * @param x 横坐标
     * @param y 纵坐标
     * @param width 宽
     * @param height 高
     * @return 是否相交
     */
    public final boolean Intersects(int x, int y, int width, int height)
    {
        return this.rect.intersects(new Rectangle(x, y, width, height));
    }


    public void AddInContainer(Container c)
    {
        this.at = c;
        c.GetWidgets().add(this);
    }

    public void Render(Renderer2D renderer) {}

}



